package com.qhx.admin.model.vo;


import com.alibaba.fastjson2.annotation.JSONField;
import com.qhx.admin.domain.BackUser;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;



@Getter
@Setter
public class LoginUserVo  implements UserDetails
{
    private BackUser user;

    private List<MenuVo> menus;

    private boolean isAdmin;

    private String browserName;
    private String osName;
    private String tokenId;
    private String loginLocation; 

    private List<String> authList;

    @JSONField(serialize = false)
    private List<SimpleGrantedAuthority> authorities;  


    public LoginUserVo(BackUser user, List<String> authList,List<MenuVo> menuVos) { 
        this.user = user;
        this.authList = authList;
        this.menus = menuVos;
    }



    @Override 
    public Collection<? extends GrantedAuthority> getAuthorities()
    {

        if(authorities != null){
            return authorities;
        }else{
            authorities = new ArrayList<>();
        }
        
     if(authList != null){
         for (String auth : authList) {
             SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(auth);
             authorities.add(simpleGrantedAuthority); 
         }
        }
        return authorities;

    }

    @Override
    public String getPassword()
    {
        return user.getPassword();
    }

    @Override
    public String getUsername()
    {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired()
    {
        return true;
    }

    @Override
    public boolean isAccountNonLocked()
    {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired()
    {
        return true;
    }

    @Override
    public boolean isEnabled()
    {
        return true;
    }
}